package com.example.xm.ui.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.xm.R;
import com.example.xm.adapter.HomeAdapter.VpTabAdapter;
import com.example.xm.ui.home.fragments.Fragment_Article;
import com.example.xm.ui.home.fragments.Fragment_Follow;
import com.example.xm.ui.home.fragments.Fragment_Photo;
import com.example.xm.ui.home.fragments.Fragment_Recommend;
import com.example.xm.ui.home.fragments.Fragment_Reward;
import com.example.xm.ui.home.fragments.Fragment_Square;
import com.example.xm.ui.home.fragments.Fragment_Video;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeFragment extends Fragment {

    @BindView(R.id.tab)
    TabLayout mTab;
    @BindView(R.id.vp)
    ViewPager mVp;
    private String[] tabs = {"关注", "推荐", "广场", "视频", "摄影", "知识文章", "悬赏"};
    private ArrayList<Fragment> fragments;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_home, null);
        ButterKnife.bind(this, inflate);
        initView();
        return inflate;
    }

    protected void initView() {
        //初始化fragment
        fragments = new ArrayList<>();
        fragments.add(new Fragment_Follow());//关注
        fragments.add(new Fragment_Recommend());//推荐
        fragments.add(new Fragment_Square());//广场
        fragments.add(new Fragment_Video());//视频
        fragments.add(new Fragment_Photo());//摄影
        fragments.add(new Fragment_Article());//知识文章
        fragments.add(new Fragment_Reward());//悬赏
        //tablayout横向滚动
        mTab.setTabMode(TabLayout.MODE_SCROLLABLE);
        //初始化ViewPager
        VpTabAdapter vpTabAdapter = new VpTabAdapter(getFragmentManager(), fragments, tabs);
        mVp.setAdapter(vpTabAdapter);
        mTab.setupWithViewPager(mVp);
    }

    /**
     * 通过这个方法跳转对应的tab已经所对应的页面
     *
     * @param pos
     */
    public void gotoTabByIndex(int pos) {
        mVp.setCurrentItem(pos);
        //((fragment_reward)(fragments.get(fragments.size()-1))).update("xxx");
    }
}